﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;
using System.Drawing;
using System.Windows.Forms;
using System.Security.AccessControl;

namespace nb.development.csmediamanager
{
    public class Sites : Dictionary<Guid, Site>
    {
        public static Sites Instance
        {
            get { return new Sites(); }
        }

        public void Add(string url, string user, string key, string name)
        {
            Guid G = Guid.NewGuid();
            this.Add(G, new Site(G, url, key, user, name));
        }

        public View ViewMode
        {
            get
            {
                if (isRegistered())
                {
                    RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\nb development\\CS Media Manager", true);
                    return (View)Enum.Parse(typeof(View), key.GetValue("ViewMode", "LargeIcon").ToString(), true);
                }
                return View.LargeIcon;
            }
            set
            {
                RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\nb development\\CS Media Manager", true);
                key.SetValue("ViewMode", value.ToString());
            }
        }

        public bool EnableEditing
        {
            get
            {
                if (isRegistered())
                {
                    RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\nb development\\CS Media Manager", true);
                    return Convert.ToBoolean(key.GetValue("EnableEditing", "false"));
                }
                return false;
            }
        }

        public Point Size
        {
            get
            {
                if (isRegistered())
                {
                    RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\nb development\\CS Media Manager", true);
                    return new Point(Convert.ToInt32(key.GetValue("width", 600)), Convert.ToInt32(key.GetValue("height", 400)));
                }
                return new Point(600, 480);
            }
            set
            {
                RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\nb development\\CS Media Manager", true);
                key.SetValue("height", value.Y);
                key.SetValue("width", value.X);
            }
        }

        public void SaveAll()
        {
            foreach (Site site in this.Values)
                site.Save();
        }

        public Sites()
        {
            RegistryKey key;
            if (isRegistered())
            {
                this.Clear();
                key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\nb development\\CS Media Manager", true);
                foreach (string s in key.GetSubKeyNames())
                    if (s.StartsWith("Site-"))
                        this.Add(new Guid(s.Remove(0, 5)), new Site(new Guid(s.Remove(0, 5))));
            }
            else
            {
                key = Registry.CurrentUser.CreateSubKey("SOFTWARE\\nb development\\CS Media Manager", RegistryKeyPermissionCheck.ReadWriteSubTree);
                key.SetValue("ViewMode", View.Tile);
                key.SetValue("height", 400);
                key.SetValue("width", 600);
                this.Clear();
            }

        }

        private bool isRegistered()
        {
            if (Registry.CurrentUser.OpenSubKey("SOFTWARE\\nb development\\CS Media Manager", RegistryKeyPermissionCheck.ReadSubTree, RegistryRights.ReadKey) != null) return true;
            return false;
        }
    }
}
